package com.nf.health.app.widget.custom_chatrow;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.widget.CircularImage;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.exceptions.HyphenateException;
import com.nf.health.app.R;
import com.nf.health.app.inquiry.docdetail.DocDetailActivity;
import com.nf.health.app.utils.ActivityUtils;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by 恩福 on 2016/10/10.
 * 医生名片 Item
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class EaseChatRowDoctorCard extends EaseChatRow {

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvSubTitle;
    private CircularImage ivDoctorHead;
    private String messageid;

    public EaseChatRowDoctorCard(Context context, EMMessage message, int position, BaseAdapter adapter,int state) {
        super(context, message, position, adapter,state);
    }

    @Override
    protected void onInflatView() {
        if(message.direct() == EMMessage.Direct.RECEIVE){
            inflater.inflate(R.layout.ease_row_received_doctor_card,this);
        }
    }

    @Override
    protected void onFindViewById() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvSubTitle = (TextView) findViewById(R.id.tv_subtitle);
        tvContent = (TextView) findViewById(R.id.tv_content);
        ivDoctorHead = (CircularImage) findViewById(R.id.iv_doctorhead);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {
        userAvatarView.setVisibility(View.INVISIBLE);
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        try {
            JSONObject object = new JSONObject("{"+txtBody.getMessage()+"}");
            String title = (String) object.get("title");
            String subtitle = (String) object.get("subtitle");
            String content = (String) object.get("content");
            String imageUrl = (String) object.get("imageurl");
            messageid = (String) object.get("messageid");

            tvTitle.setText(title);
            tvSubTitle.setText(subtitle);
            tvContent.setText(content);
            Glide.with(getContext()).load(imageUrl).into(ivDoctorHead);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat){
            try {
                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onBubbleClick() {
        if(!TextUtils.isEmpty(messageid)) {
            Bundle docDetailBundle = new Bundle();
            docDetailBundle.putString(DocDetailActivity.DOCTORID, messageid);//messageid即doctorid
            ActivityUtils.showActivity(context, DocDetailActivity.class, docDetailBundle);
        }
    }
}
